package cn.iocoder.yudao.module.ai.service.operationscene;

import cn.iocoder.yudao.framework.mybatis.core.type.StringListTypeHandler;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.operationscene.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.operationscene.OperationSceneDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.operationscene.OperationSceneMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 运营中心-场景监控 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class OperationSceneServiceImpl implements OperationSceneService {

    @Resource
    private OperationSceneMapper operationSceneMapper;

    @Override
    public Long createOperationScene(OperationSceneSaveReqVO createReqVO) {
        // 插入
        OperationSceneDO operationScene = BeanUtils.toBean(createReqVO, OperationSceneDO.class);
        operationSceneMapper.insert(operationScene);
        // 返回
        return operationScene.getId();
    }

    @Override
    public void updateOperationScene(OperationSceneSaveReqVO updateReqVO) {
        // 校验存在
        validateOperationSceneExists(updateReqVO.getId());
        // 更新
        OperationSceneDO updateObj = BeanUtils.toBean(updateReqVO, OperationSceneDO.class);
        operationSceneMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperationScene(Long id) {
        // 校验存在
        validateOperationSceneExists(id);
        // 删除
        operationSceneMapper.deleteById(id);
    }

    private void validateOperationSceneExists(Long id) {
        if (operationSceneMapper.selectById(id) == null) {
            throw exception(OPERATION_SCENE_NOT_EXISTS);
        }
    }

    @Override
    public OperationSceneDO getOperationScene(Long id) {
        return operationSceneMapper.selectById(id);
    }

    @Override
    public PageResult<OperationSceneDO> getOperationScenePage(OperationScenePageReqVO pageReqVO) {
        return operationSceneMapper.selectPage(pageReqVO);
    }

    @Override
    public OperationSceneRespCountVO countOperationScene(List<String> companyIds,OperationSceneSimplePageReqVO pageReqVO) {
        return operationSceneMapper.countOperationScene(companyIds,pageReqVO.getIndustryAffiliation(),pageReqVO.getSceneClassification());
    }
}